<?php

class E_DBConnectionPool 
{
	private   $_DB = null;
	private function __construct()
	{
		
	}
	public static function  getDbHandle($nocache = 0)
	{
		if(!Zend_Registry::isRegistered('DB'))
		{
			$zend_config = Zend_Registry::get("zend_config");
		
			if($zend_config->database->db == "zend")
			{
				$DB = Zend_Db::factory($zend_config->database->adapter1,$zend_config->database->params);
			}
			else 
			{
				$DB = D_DB::factory($zend_config);
			}
			
			Zend_Registry::set("DB", $DB);
			//$DB->query("set names utf8");
		}
		else
		{
			$DB = Zend_Registry::get("DB");
		}
		
//		if($nocache == 1)
//		{
//			return $DB;
//		}
		$DBCP = new self();
		$DBCP->_DB = $DB; 
		return $DBCP;
	}
	public static function getIDGeneratorByDb()
	{
		if(!Zend_Registry::isRegistered('IDGenerator'))
		{
			$IDGenerator = new E_IDGeneratorByDb();
			Zend_Registry::set("IDGenerator", $IDGenerator);
			//$this->_DB->query("set names utf8");
		}
		else
		{	
			
			$IDGenerator = Zend_Registry::get("IDGenerator");
		}
		return $IDGenerator;
	}
//	public function fetchFromCache($sql)
//	{		
//		if(Zend_Registry::isRegistered('sqlcache'))
//		{  
//			$key = md5($sql);
//			$cache = Zend_Registry::get('sqlcache');
//			if(!$data = $cache->load($key))
//			{
//				$data = $this->fetch($sql);
//				if(!empty($data))
//				$cache->save($data, $key, array($data['id'])); 
//			}
//		}
//		else 
//		{
//			$data = $this->fetch($sql);
//			
//		}
//		return $data;
//	}
//	public function fetchAllFromCache($sql, $table)
//	{		
//		if(Zend_Registry::isRegistered('sqlcache'))
//		{  
//			$key = md5($sql);
//			$cache = Zend_Registry::get('sqlcache');
//			if(!$data = $cache->load($key))
//			{
//				$data = $this->fetchAll($sql);
//				if(!empty($data))
//				{
//					foreach($data as $v)
//					{
//						$ids[] = $v['id'];
//					}
//					$ids[] = $table;
//					$cache->save($data, $key, $ids);
//				} 
//			}
//		}
//		else 
//		{
//			$data = $this->fetchAll($sql);
//			
//		}
//		return $data;
//	}	
	public function fetchAll($sql, $bind=array())
	{
		if(Zend_Registry::isRegistered('sqlcache'))
		{  
			$cache = Zend_Registry::get('sqlcache');
			if(!$result = $cache->load(md5($sql))) 
			{
				$result = $this->_DB->fetchAll($sql, $bind);
				$cache->save($result, md5($sql));	
			}
			
		}
		else
		{
			
				$result = $this->_DB->fetchAll($sql, $bind);
		}
		return $result;
	}
	public function fetch($sql, $bind=array())
	{
		if(Zend_Registry::isRegistered('sqlcache'))
		{  
			$cache = Zend_Registry::get('sqlcache');
			if(!$result = $cache->load(md5($sql))) 
			{
				$result = $this->_DB->fetchRow($sql, $bind);
				$cache->save($result, md5($sql));	
			}
		}
		else
		{
				$result = $this->_DB->fetchRow($sql, $bind);
		}
		return $result;
	}
	public function insert($sql, $bind = array())
	{
		return $this->_DB->query($sql, $bind);
	}
	public function removeTag($id)
	{
		if(Zend_Registry::isRegistered('sqlcache'))
		{  
			$cache = Zend_Registry::get('sqlcache');
			$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array($id));
		}
	}
	public function update($sql, $bind = array())
	{
		return $this->_DB->query($sql, $bind);
	}
	public function query($sql, $bind = array())
	{
		return $this->_DB->query($sql, $bind);
	}
	public function get_num()
	{
		return $this->_DB->get_num();
	}
}

?>